<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Professionals extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('portal', $this->session->userdata['user_lang']);
		$this->lang->load('professionals', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_model', 'candidate');
	}
	
	public function index()
	{
		$page_data = array();
		$data = $_POST;
		$page_data['main_content'] = 'professionals/search';
		$page_data['page_title'] = $this->lang->line('professionals_title');
		
		if (!isset($this->session->userdata['idmember']))
		{ 
			$page_data['header_base'] = '<a href="'.base_url().'register" class="button"><span>'.$this->lang->line('professionals_registerbutton').'</span></a>
			<span class="highlighttext">'.$this->lang->line('professionals_registertext').'</span>';
		}
		
		$count_professionals = '130.000';
		$page_data['available_professionals_title'] = sprintf($this->lang->line('professionals_availableprofessionalstitle'), $count_professionals);
		
		$this->load->view('template-search', $page_data);
	}
	
	public function search()
	{
		$data = $_POST;
		$data['limit'] = 15;

		$professionals_list = $this->candidate->search($data);
		
		$grid_professionals = array();
        
		foreach ($professionals_list->result() as $row)
		{
			$company_image = $this->get_member_link($row->idmember, 1, 'image');
			$company_link = $this->get_member_link($row->idmember, 1);
			
			$professional_content = '<div class="professional-content">' .
					$company_image . '' . 
					'<center>' . $company_link . '</center>' .
					'<div class="professional-date">' . 
						sprintf(format_date($row->date, true, true, true), $this->lang->line('global_in'), $this->lang->line('global_at')) .
					'</div>' .
					'</div>'; 
			
			$grid_professionals[] = '<div class="row">
								<div class="row-content">
									<span>
										<span class="gdesc">'.$professional_content.'</span>
									</span>
									<div class="gtool">
									</div>
								</div>
							</div>';
		}
		
		
		if (!$grid_professionals) {
			 $data['grid_professionals'] = '<center>'.$this->lang->line('professionals_noresults').'</center>';
		} else {
			 $data['grid_professionals'] = implode('', $grid_professionals);
		}

		$count_professionals = $this->candidate->get_count_professionals($data);
		
		if ($count_professionals > $data['limit'])
		{
			$pagination = '<div class="both"><center> '.$this->lang->line('global_pagination').' <select id="professionals_offset" class="input" onchange="Professionals.processSearch();">';
			$count_links = $count_professionals / $data['limit'];
			
			for ($i = 1; $i <= $count_links; $i++)
			{
				$pagination .= '<option value="'.($i - 1).'">'.$i.'</option>';
			}
			
			$pagination .= '</select></center></div>';
			
			$pagination .= '<script type="text/javascript">$("#professionals_offset").val("'.$data['offset'].'");</script>';
			
			$data['grid_professionals'] .= '<br/>' . $pagination;
		}
        
        exitHTML($data['grid_professionals']);
	}
}

?>